/**
 * 
 */
package com.visionet.school.app.web.security.password;

import java.security.MessageDigest;

/**
 * @author Deng
 *
 */
public final class Md5Encoder {
	
	private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
	
	public static String encoding(String string) throws Exception{
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		byte[] content = string.getBytes("utf-8");
		String hexString = toHexString(md5.digest(content));
		return hexString;
	}
	
	private static String toHexString(byte [] content){
		StringBuilder stringBuilder = new StringBuilder();
		for(int i =0;i<content.length;i++){
			stringBuilder.append(byte2Hex(content[i]));
		}
		return stringBuilder.toString();
	}
	
	private static String byte2Hex(byte bt){
		return HEX_DIGITS[(bt & 0xf0) >> 4] + "" + HEX_DIGITS[(bt & 0x0f) >> 0];
	}
	
}
